﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using DTO;
using CrystalDecisions.CrystalReports.Engine;
//using DocumentFormat.OpenXml.Packaging; 
//using DocumentFormat.OpenXml.Spreadsheet; 
//using DocumentFormat.OpenXml;

namespace WebChamCong
{
    public partial class EmployeeDetails : System.Web.UI.Page
    {
        private int _userId;

        private DateTime _currentDate;

        private NhanVienDTO _currentUser;
        double TotalMealAfter=0 ;
        double TotalMealEven=0 ;
        double TotalTimeStandar=0 ;
        double TotalWorkedTime=0 ;
        double TotalLateTime=0;
        double TotalEarlyTime=0;
        double TotalEatLateTime=0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] == null || Request.QueryString["month"] == null || Request.QueryString["year"] == null)
            {
                Response.Redirect("index.aspx");
            }

            int localId = Convert.ToInt32(Request.QueryString["id"]);

            int localMonth = Convert.ToInt32(Request.QueryString["month"]);

            int localYear = Convert.ToInt32(Request.QueryString["year"]);

            int localDay = 1;

            DateTime localDate = new DateTime(localYear, localMonth, localDay);

            this._userId = localId;

            this._currentDate = localDate;

            NhanVienDTO objNhanVien = BUS.NhanVienBUS.GetNhanViensByID(localId);
            this._currentUser = objNhanVien;

            if(!Page.IsPostBack)
            {
                //NhanVienDTO objNhanVien = BUS.NhanVienBUS.GetNhanViensByID(localId);
                this.lb_empName.Text = objNhanVien.Name;
                this.lb_departName.Text = objNhanVien.DepartmentName;
                this.lb_datedshift.Text = localDate.Month.ToString() + "/" + localDate.Year.ToString();
                string annouce = "Giờ ăn trưa bắt đầu tính từ: " + new DateTime(2000, 1, 1, 11, 0, 0).TimeOfDay.ToString() + " - " + new DateTime(2000, 1, 1, 14, 0, 0).TimeOfDay.ToString();
                annouce += ". Giờ ăn tối bắt đầu tính từ  : " + new DateTime(2000, 1, 1, 17, 0, 0).TimeOfDay.ToString() +
                           " - " + new DateTime(2000, 1, 1, 20, 0, 0).TimeOfDay.ToString();
                this.lb_annoucement.Text = annouce;
                //this._currentUser = objNhanVien;
                
            }
            LoadGridView();
        }

        protected void LoadGridView()
        {
            int localId = this._userId;
            DateTime localDate = this._currentDate;

            this.gv_detailedEmp.DataSource = BUS.LichBUS.GetUserDetail(localId, localDate, ref TotalMealAfter, ref TotalMealEven, ref TotalTimeStandar, ref TotalWorkedTime, ref TotalLateTime, ref TotalEarlyTime, ref TotalEatLateTime);
            this.gv_detailedEmp.DataBind();
        }


        protected void loadHistory(int userId, DateTime time)
        {
            int localUserId = userId;
            DateTime localTime = time;

            List<Check> checkInOutsByUserAndDate = BUS.LichBUS.GetCheckInOutsByUserAndDate(localUserId, localTime);
            this.gv_history.DataSource = checkInOutsByUserAndDate;
            this.gv_history.DataBind();
        }

        protected void gv_detailedEmp_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("LoadHistory"))
            {
                int localUserId = this._userId;
                DateTime localDate = this._currentDate;
                LinkButton te = (LinkButton)e.CommandSource;
                DateTime de = new DateTime(localDate.Year, localDate.Month, Convert.ToInt32(te.Text));
                this.lb_Date.Text = "Ngày : " + de.ToShortDateString().ToString();
                loadHistory(localUserId, de);
                this.ModalPopupExtender1.Show();
            }
        }

        protected void gv_detailedEmp_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                Label lb1 = new Label();
                lb1.Text = Properties.Settings.Default.tittle_day;
                e.Row.Cells[0].Controls.Add(lb1);
                Label lb2 = new Label();
                lb2.Text = Properties.Settings.Default.tittle_Shift;
                e.Row.Cells[1].Controls.Add(lb2);
                Label lb17 = new Label();
                lb17.Text = Properties.Settings.Default.tittle_Hday;
                e.Row.Cells[2].Controls.Add(lb17);
                Label lb3 = new Label();
                lb3.Text = Properties.Settings.Default.tittle_hourin;
                e.Row.Cells[3].Controls.Add(lb3);
                Label lb4 = new Label();
                lb4.Text = Properties.Settings.Default.tittle_TimeMealOut;
                e.Row.Cells[4].Controls.Add(lb4);
                Label lb5 = new Label();
                lb5.Text = Properties.Settings.Default.tittle_TimeMealIn;
                e.Row.Cells[5].Controls.Add(lb5);
                Label lb6 = new Label();
                lb6.Text = Properties.Settings.Default.tittle_TimeEveOut;
                e.Row.Cells[6].Controls.Add(lb6);
                Label lb7 = new Label();
                lb7.Text = Properties.Settings.Default.tittle_TimeEveIn;
                e.Row.Cells[7].Controls.Add(lb7);
                Label lb8 = new Label();
                lb8.Text = Properties.Settings.Default.tittle_TimeOut;
                e.Row.Cells[8].Controls.Add(lb8);
                Label lb9 = new Label();
                lb9.Text = Properties.Settings.Default.tittle_TLMeal;
                e.Row.Cells[9].Controls.Add(lb9);
                Label lb10 = new Label();
                lb10.Text = Properties.Settings.Default.tittle_TLNight;
                e.Row.Cells[10].Controls.Add(lb10);
                Label lb11 = new Label();
                lb11.Text = Properties.Settings.Default.tittle_TLOut;
                e.Row.Cells[11].Controls.Add(lb11);
                Label lb12 = new Label();
                lb12.Text = Properties.Settings.Default.tittle_LateTime;
                e.Row.Cells[12].Controls.Add(lb12);
                Label lb13 = new Label();
                lb13.Text = Properties.Settings.Default.tittle_EarlyTime;
                e.Row.Cells[13].Controls.Add(lb13);
                Label lb14 = new Label();
                lb14.Text = Properties.Settings.Default.tittle_eatLate;
                e.Row.Cells[14].Controls.Add(lb14);
                Label lb15 = new Label();
                lb15.Text = Properties.Settings.Default.tittle_TimeStandar;
                e.Row.Cells[15].Controls.Add(lb15);
                Label lb16 = new Label();
                lb16.Text = Properties.Settings.Default.tittle_TimeWork;
                e.Row.Cells[16].Controls.Add(lb16);

            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lb=new Label();
                lb.Text=TotalMealAfter.ToString();
                e.Row.Cells[8].Controls.Add(lb);
                Label lb1 = new Label();
                lb1.Text = TotalMealEven.ToString();
                e.Row.Cells[9].Controls.Add(lb1);
                Label lb2 = new Label();
                lb2.Text = TotalTimeStandar.ToString();
                e.Row.Cells[14].Controls.Add(lb2);
                Label lb3 = new Label();
                lb3.Text = TotalWorkedTime.ToString();
                e.Row.Cells[15].Controls.Add(lb3);
                Label lb4 = new Label();
                lb4.Text= TotalLateTime.ToString();
                e.Row.Cells[11].Controls.Add(lb4);
                Label lb5 = new Label();
                lb5.Text= TotalEarlyTime.ToString();
                e.Row.Cells[12].Controls.Add(lb5);
                Label lb6 = new Label();
                lb6.Text=TotalEatLateTime.ToString();
                e.Row.Cells[13].Controls.Add(lb6);
            }
        }
        private void Export_Employee(int empId, DateTime date)
        {
            int localEmpId = empId;
            DateTime localDate = date;
            double localTotalMealAfter = TotalMealAfter;
            double localTotalMealEven = TotalMealEven;
            double localTotalTimeStandar = TotalTimeStandar;
            double localTotalWorkedTime = TotalWorkedTime;
            double locatTotalLateTime = TotalLateTime;
            double localTotalEarlyTime = TotalEarlyTime;
            double localTotalEatLateTime = TotalEatLateTime;
            List<DTO.UserDetail> localUserDetails = BUS.LichBUS.GetUserDetail(localEmpId, localDate, ref TotalMealAfter, ref TotalMealEven, ref TotalTimeStandar, ref TotalWorkedTime, ref TotalLateTime, ref TotalEarlyTime, ref TotalEatLateTime);
            string fileName = Server.MapPath("temp_excels/test.xls");
            ReportDocument rpObject = new ReportDocument();

            rpObject.Load(Server.MapPath("data/rp_employee.rpt"));


            //table o day
            DataTable result = BUS.LichBUS.ConvertListToDataTable(localUserDetails, ref TotalMealAfter, ref TotalMealEven, ref TotalTimeStandar, ref TotalWorkedTime, ref TotalLateTime, ref TotalEarlyTime, ref TotalEatLateTime);

            rpObject.SetDataSource(result);
            DTO.BoPhanDTO bp = BUS.BoPhanBUS.GetBoPhanById(this._currentUser.DepartmentId);
            rpObject.SetParameterValue("empName", this._currentUser.FullName);
            rpObject.SetParameterValue("empDepart", bp.DepartmentName);
            rpObject.SetParameterValue("empDate", localDate);

            rpObject.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.Excel, fileName);

            FileInfo file = new FileInfo(fileName);

            if (file.Exists)
            {
                //Response.Clear();
                Response.Buffer = true;
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(file.FullName);
            }
        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }
       
        protected void Bt_export_Click(object sender, EventArgs e)
        {
            Export_Employee(this._userId, this._currentDate);
        }
    }
}